﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using SealManagement.Common;
using SealManagement.Extensions;
using SealManagement.Models;

namespace SealManagement.SqlData
{
    public static class DbInitializer
    {
        public static void Initialize(SignContext context, IOptions<AppSettings> options)
        {
            try
            {
                //context.Database.EnsureDeleted();
                context.Database.EnsureCreated();
            }
            catch
            {
                Console.WriteLine("failed to construct database!");
            }

            if (!context.Users.Any())//初始化用户表
            {
                var cerPath = Path.Combine(Define.Lic, options.Value.AdminCertPath);
                byte[] certBytes = null;
                using (FileStream fsRead = new FileStream(cerPath, FileMode.Open))
                {
                    int fsLen = (int)fsRead.Length;
                    certBytes = new byte[fsLen];
                    int r = fsRead.Read(certBytes, 0, certBytes.Length);
                }
                X509Certificate2 cert = new X509Certificate2(certBytes);
                var serialNum = cert.SerialNumber;
                var certBase64 = Convert.ToBase64String(cert.RawData);

                var users = new User[]
                {
                    new User{
                        ArrayNo =serialNum,
                        Certificator =certBase64,
                        CreateDate =DateTime.Now.ToString(),
                        IsDelete =0,
                        UserName ="admin",
                        Remark ="123456"
                    }
                };
                context.Users.AddRange(users);
            }
            if (!context.MenuItems.Any())//初始化菜单表
            {
                var menus = new MenuItem[]
                {
                    new MenuItem{ MenuPath="#", MenuString="管理员管理", ParentId=0, UserType=(int)userType.User},//1
                    new MenuItem{ MenuPath="/User/index", MenuString="查看管理员", ParentId=1, UserType=(int)userType.User},//2
                    new MenuItem{ MenuPath="/User/create", MenuString="添加管理员", ParentId=1, UserType=(int)userType.User},//3
                    new MenuItem{ MenuPath="#", MenuString="制章人管理", ParentId=0, UserType=(int)userType.User},//4 
                    new MenuItem{ MenuPath="/signmaker/index", MenuString="查看制章人", ParentId=4, UserType=(int)userType.User},//5
                    new MenuItem{ MenuPath="/signmaker/create", MenuString="添加制章人", ParentId=4, UserType=(int)userType.User},//6
                    new MenuItem{ MenuPath="#", MenuString="印章管理", ParentId=0, UserType=(int)userType.SignMaker},//7
                    new MenuItem{ MenuPath="/producesign/create", MenuString="制章", ParentId=7, UserType=(int)userType.SignMaker},
                    new MenuItem{ MenuPath="/producesign/verify", MenuString="验章", ParentId=7, UserType=(int)userType.SignMaker},
                    new MenuItem{ MenuPath="/producesign/restore", MenuString="恢复印章", ParentId=7, UserType=(int)userType.SignMaker},
                    new MenuItem{ MenuPath="#", MenuString="日志管理", ParentId=0, UserType=(int)userType.SignMaker},//11
                    new MenuItem{ MenuPath="/infolog/index", MenuString="查看日志", ParentId=11, UserType=(int)userType.SignMaker},
                    new MenuItem{ MenuPath="#", MenuString="日志管理", ParentId=0, UserType=(int)userType.User},//13
                    new MenuItem{ MenuPath="/infolog/index", MenuString="查看日志", ParentId=13, UserType=(int)userType.User},
                    new MenuItem{ MenuPath="/infolog/backup", MenuString="备份恢复全库", ParentId=13, UserType=(int)userType.User},
                };
                context.MenuItems.AddRange(menus);
            }
            context.SaveChangesAsync();
        }
    }
}
